package com.example.lyl.yuejian.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.example.lyl.yuejian.R;
import com.example.lyl.yuejian.adapter.PhotoLvAdapter;
import com.example.lyl.yuejian.bean.NotifyEventBean;
import com.example.lyl.yuejian.bean.PhotoLvBean;
import com.example.lyl.yuejian.bean.ResultBean;
import com.example.lyl.yuejian.net.NetTool;
import com.example.lyl.yuejian.util.SharePreferenceUtils;
import com.example.lyl.yuejian.view.photo.AddPhotoActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class PhotoFragment extends Fragment {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    Unbinder unbinder;
    @BindView(R.id.photoLv)
    ListView photoLv;
    @BindView(R.id.pl_et)
    EditText plEt;
    @BindView(R.id.plsend_btn)
    Button plsendBtn;
    @BindView(R.id.botomLl)
    LinearLayout botomLl;
    private PhotoLvAdapter adapter;
    private int pid;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_photo, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        toolbar.setTitle("晒图");

        adapter = new PhotoLvAdapter();
        adapter.setPlflag(true);
        adapter.setListener(new PhotoLvAdapter.PlBtnClickListener() {
            @Override
            public void clickListener(int pid) {
                botomLl.setVisibility(View.VISIBLE);
                PhotoFragment.this.pid = pid;
            }
        });
        photoLv.setAdapter(adapter);
        getData(adapter);

    }

    private void getData(final PhotoLvAdapter adapter) {
        NetTool.getInstance().getApi()
                .photoListRequest()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<PhotoLvBean>() {
                    @Override
                    public void accept(PhotoLvBean photoLvBean) throws Exception {
                        adapter.setPhotoLvBean(photoLvBean);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                });
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        getActivity().getMenuInflater().inflate(R.menu.phototoolbar, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.photoicon:
                startActivity(new Intent(getContext(), AddPhotoActivity.class));
                break;
        }
        return true;
    }

    @Subscribe
    public void onEventMainThread(NotifyEventBean event) {
        if (event.getMsg().equals("刷新晒图列表")) {
            getData(adapter);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

    @OnClick(R.id.plsend_btn)
    public void onViewClicked() {
        botomLl.setVisibility(View.GONE);
        NetTool.getInstance().getApi()
                .addPinglun(pid, SharePreferenceUtils.getInt(getContext(),"uid",0),plEt.getText().toString())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<ResultBean>() {
                    @Override
                    public void accept(ResultBean resultBean) throws Exception {
                        getData(adapter);
                        botomLl.setVisibility(View.GONE);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        Toast.makeText(getContext(), "检查网络", Toast.LENGTH_SHORT).show();
                    }
                });
    }
}
